package com.fangcun.common.async;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.scheduling.annotation.EnableAsync;
import org.springframework.stereotype.Component;

import com.fangcun.common.util.tencent.TencentUtils;
import com.fangcun.entity.User;
import com.fangcun.mapper.UserMapper;

/**
 * @作者： 陳曉鬆 @时间： 2020年8月14日 - 下午3:06:11 @description：->
 */
@Component
@EnableAsync
public class AsyncMethod {

	@Autowired
	private TencentUtils tencentUtils;
	@Autowired
	private UserMapper userMapper;

	// 设置Unionid
	@Async
	public void settingUnionid(Long userId, String appid, String openid, String transactionId) {
		try {
			User user = userMapper.selectByPrimaryKey(userId);
			if (user != null && StringUtils.isEmpty(user.getUnionid())) {
				String uuid = tencentUtils.getUnionid(appid, openid, transactionId);
				if (!StringUtils.isEmpty(uuid)) {
					User updateUser = new User();
					updateUser.setId(userId);
					updateUser.setUnionid(uuid);
					userMapper.updateByPrimaryKeySelective(updateUser);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

}
